package cn.dengta.webapp.trade.model;

import java.util.*;

import cn.dengta.common.model.*;
import cn.dengta.context.model.OptionExt;
import lombok.Getter;
import me.codeplayer.util.Cmp;

/**
 * 亲密度级别
 */
@Getter
public enum IntimacyLevel implements ValueProxyImpl<IntimacyLevel, Integer> {

	L0(0, 0, 0, State.PROTECTED),
	/** 聊天免费（条） */
	L1(1, 20, 10, "相遇"),
	/** 语音免费（分） */
	L2(2, 200, 100, "相识"),
	/** 视频免费（分） */
	L3(3, 2000, 1000, "相知"),
	/** 聊天视频语音优惠折扣（%） */
	L4(4, 20000, 10000, "相惜"),
	/** 礼物优惠折扣（%） */
	L5(5, 200000, 100000, "相伴"),
	//
	;
	/** 亲密度等级 */
	public final Integer value;
	/** 需要消费的钻石数 */
	public final double score;
	/** 亲密度℃ */
	public final Integer intimacy;
	/** 国内版本标签 */
	public final String interLabel;
	final ValueProxy<IntimacyLevel, Integer> proxy;

	IntimacyLevel(Integer value, Integer score, Integer intimacy, State state) {
		this(value, score, intimacy, state, null);
	}

	IntimacyLevel(Integer value, Integer score, Integer intimacy, String interLabel) {
		this(value, score, intimacy, State.PUBLIC, interLabel);
	}

	// 构造函数，枚举类型只能为私有
	IntimacyLevel(Integer value, Integer score, Integer intimacy, State state, String interLabel) {
		this.value = value;
		this.score = score;
		this.intimacy = intimacy;
		this.interLabel = interLabel;
		this.proxy = new ValueProxy<>(this, state, value, name());
	}

	public static final IntimacyLevel[] CACHE = Arrays.stream(values()).filter(m -> m.getProxy().state == State.PUBLIC).toArray(IntimacyLevel[]::new);

	public static IntimacyLevel[] getValues() {
		return ValueProxy.getCachedArray(IntimacyLevel.class, IntimacyLevel::values);
	}

	public boolean ge(IntimacyLevel level) {
		return level != null && value >= level.value;
	}

	public boolean gt(IntimacyLevel level) {
		return level != null && value > level.value;
	}

	public static IntimacyLevel calcLevel(final Integer score) {
		if (score != null) {
			IntimacyLevel[] values = CACHE;
			for (int i = values.length - 1; i >= 0; i--) {
				IntimacyLevel level = values[i];
				if (score >= level.score) {
					return level;
				}
			}
		}
		return IntimacyLevel.L0;
	}

	public static int calcIntimacy(final Integer score) {
		IntimacyLevel level = calcLevel(score);
		if (IntimacyLevel.L0 == level) {  // 0-10:每5钻增加1亲密
			return Math.min(Cmp.nullToZero(score) / 2, 99);
		}
		Integer value = level.getValue();
		if (value >= CACHE.length) {
			return level.intimacy + (score - (int) level.score) / 2;
		}
		IntimacyLevel nextLevel = of(value + 1);
		return Math.min(level.intimacy + (int) ((score - level.score) / ((nextLevel.score - level.score) / (nextLevel.intimacy - level.intimacy))), nextLevel.getIntimacy() - 1);
	}

	public static List<OptionExt<Integer>> getOptions() {
		final List<OptionExt<Integer>> options = new ArrayList<>(CACHE.length);
		for (IntimacyLevel level : CACHE) {
			OptionExt<Integer> op = new OptionExt<>(level.getValue(), level.getLabel(), null);
			Map<String, Object> ext = new HashMap<>();
			ext.put("intimacy", level.getIntimacy());
			ext.put("coins", level.getScore());
			op.setExt(ext);
			options.add(op);
		}
		return options;
	}

	public static IntimacyLevel of(Integer value) {
		return L1.getValueOf(value);
	}
}
